<template>
	<view class="container">
		<uni-list>
			<uni-list-item title="启用抽奖" :show-switch="true" :switchChecked="lotteryUsing"
				@switchChange="switchLottery"></uni-list-item>
		</uni-list>
		<view class="gaps">

		</view>
		<view class="prize-area">
			<uni-list>
				<uni-list-item v-for="item in prizes" :key="item.id" clickable="true" @click="prizeClick(item)">
					<template v-slot:header>
						<view class="slot-box">
							<image class="slot-image" :src="item.imageUrl || '/static/logo.png'" mode="scaleToFill"></image>
						</view>
					</template>
					<template v-slot:body>
						<view class="prize-info" v-if="item.type == 1">
							<view class="prize-name">{{item.name}}</view>
							<view class="prize-weight">权重：{{item.weight}}</view>
							<view class="prize-stock">库存：{{item.stock}}</view>
						</view>
						<view class="prize-info" v-else-if="item.type== 2">
							<view class="prize-name">{{item.name}}</view>
							<view class="prize-price">面值：{{item.price}}分</view>
							<view class="prize-stock">库存：{{item.stock}}</view>
							<view class="prize-weight">权重：{{item.weight}}</view>
							<view class="prize-weight">有效期：{{item.expirationDate}}小时</view>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
			<!-- <uni-list>
				<uni-list-item v-for="item in coupons" :key="item.id" clickable="true" @click="couponClick(item)">
					<template v-slot:header>
						<view class="slot-box">
							<image class="slot-image" src="/static/logo.png" mode="scaleToFill"></image>
						</view>
					</template>
					<template v-slot:body>
						<view class="coupon-info">
							<view class="prize-name">{{item.name}}</view>
							<view class="prize-price">面值：{{item.price}}分</view>
							<view class="prize-stock">库存：{{item.stock}}</view>
							<view class="prize-weight">权重：{{item.weight}}</view>
							<view class="prize-weight">有效期：{{item.expirationDate}}小时</view>
						</view>
					</template>
					
					
				</uni-list-item>
			</uni-list> -->
			
			
		</view>
		<view class="prize-add">
			<u-button type="success" text="添加奖品" @click="addPrize"></u-button>
		</view>

	</view>
</template>

<script>
	import {
		enableLottery,
		getMarketStatus,
		getPrizes,
		editPrizes,
		delPrize,
		// getCoupon,
		// delCoupon
	} from '@/api/shops/lotteryManage/lotteryManage.js'
	export default {
		data() {
			return {
				prizes: [],
				coupons:[],
				lotteryUsing: false,
				acctionList: ['编辑', '删除'],
				shopId: '',
			}
		},
		onLoad(options) {
			this.shopId = options.shopId
			this.init()
		},
		methods: {
			init() {
				getMarketStatus().then((res) => {
					this.lotteryUsing = res.lotteryStatus === 1
				})
				this.getPrizesIni()
			},
			getPrizesIni() {
				getPrizes(1).then((res) => {
					this.prizes = res
				})
				// getCoupon().then((res) => {
				// 	this.coupons = res
				// })
			},
			prizeClick(prize) {
				let _this = this
				uni.showActionSheet({
					itemList: ['删除'],
					success: (res) => {
						if(res.tapIndex == 0) {
							uni.showModal({
								title: `确定删除 [${prize.name}]`,
								success: (r) => {
									if (r.confirm) {
										delPrize(prize.id, prize.times).then((p) => {
											_this.prizes = _this.prizes.filter(e => e
												.id != prize.id)
										})
									}
								}
							})
						} 
						
						
						// switch (res.tapIndex) {
						// 	case 0:
						// 		uni.navigateTo({
						// 			url: `/subpkg/shops/addPrize/addPrize?id=${prize.id}&shopId=${_this.shopId}&times=1`,
						// 			events: {
						// 				editChange: function(changeValue) {
						// 					_this.prizes.forEach(e => {
						// 						if (e.id === changeValue.data.id) {
						// 							Object.assign(e, changeValue.data)
						// 							return
						// 						}
						// 					})
						// 				}
						// 			}
						// 		})
						// 		break
						// 	case 1:
						// 		uni.showModal({
						// 			title: `确定删除 [${prize.name}]`,
						// 			success: (r) => {
						// 				if (r.confirm) {
						// 					delPrize(prize.id, prize.times).then((p) => {
						// 						_this.prizes = _this.prizes.filter(e => e
						// 							.id != prize.id)
						// 					})
						// 				}
						// 			}
						// 		})
						// }
					}
				})
			},
			couponClick(coupon) {
				let _this = this
				uni.showActionSheet({
					itemList:['删除'],
					success: (res) => {
						if(res.tapIndex == 0) {
							uni.showModal({
								title:'确定删除吗',
								success: (r) => {
									if(r.confirm) {
										delCoupon(coupon.id).then((result) => {
											_this.coupons = _this.coupons.filter((item) => {
												return item.id !== coupon.id
											})
										})
									}
								}
							})
						}
					}
				})
			},

			addPrize() {
				let _this = this
				uni.showActionSheet({
					itemList: ['优惠券', '普通奖品'],
					success: (res) => {
						if (res.tapIndex == 0) {
							return uni.navigateTo({
								// 优惠券 
								url: `/subpkg/shops/addCoupon/addCoupon?shopId=${_this.shopId}&times=1`,
								events: {
									prizeListChange: function(v) {
										_this.getPrizesIni()
									}
								}
							})
						}
						if (res.tapIndex == 1) {
							return uni.navigateTo({
								url: `/subpkg/shops/addPrize/addPrize?shopId=${_this.shopId}&times=1`,
								events: {
									prizeListChange: function(v) {
										_this.getPrizesIni()
									}
								}
							})
						}
					}
				})
			},
			checkboxChange(e) {
				console.log(e);
			},
			switchLottery(e) {
				enableLottery(e.value ? 1 : 0).then((res) => {
					this.lotteryUsing = e.value
					uni.showToast({
						title: '切换成功',
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.prize-area {
		padding-bottom: 60px;
	}

	.slot-image {
		width: 80px;
		height: 80px;
	}

	.prize-info {
		margin-left: 10px;

		.prize-name {
			font-size: 18px;
		}
	}

	.prize-add {
		height: 50px;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;

		.lottery-tips {
			color: #999;
			font-size: 13px;
			text-decoration-line: underline;
			text-decoration-color: yellow;
		}
	}
</style>